<?php
namespace app\agentmobile\controller;

use app\lib\exception\LoginException;
use app\lib\exception\SuccessMessage;
use think\Request;
use app\agent\model\Agent;
use app\agentmobile\model\AgentOrder as OrderModel;
use app\agent\model\Key;

class AgentOrder extends Base {
    public function Index() {
        $num = Key::where('is_active', 'eq', '1')->where(array('agent_id' => session('agentId')))->count();
        $this->assign('total_key', Key::where('is_active', 'neq', '-1')->where(array('agent_id' => session('agentId')))->count());
        $this->assign('is_active', $num);
        return view();
    }

    //代理商订购激活码
    public function agentOrder() {
        if(Request::instance()->post()) {
            $data = Request::instance()->post();
            if(!is_numeric($data['num'])) {
                throw new LoginException([
                    'msg' => '输入的数量不合法'
                ]);
            }
            $data['order_num'] = date("YmdHi").rand(1000, 9999);
            $data['aid'] = session('agentId');
            $data['uper_id'] = Agent::where('id', session('agentId'))->value('uper_id');

            if(OrderModel::create($data, true)) {
                throw new SuccessMessage([
                    'msg' => '订购成功'
                ]);
            }else {
                throw new LoginException([
                    'msg' => '订购失败'
                ]);
            }

        }
    }

    //代理商的订购单列表(待发货)
    public function myOrderList() {
        $condition = array(
            'a.uper_id' => session('agentId'),
            'a.is_delivery' => 0,
            'a.status' => 1,
        );
        $list = $this->getAgentInfo($condition);

        $this->assign('list', $list);
        return view();
    }

    //代理商订单的详情页
    public function myOrderDetail() {
        $order_num = Request::instance()->param('order_num');

        if(Request::instance()->post()){
            $data = Request::instance()->post();
            //先把激活码更新到用户名下
            $res = (new Key())->distributeCode($data['id'], $data['name'], $data['num']);

            if(intval($res['num']) > 0) {
                OrderModel::where('order_num', $data['order_num'])->update(array('is_delivery' => 1,'update_time' => time()));
                throw new SuccessMessage([
                    'msg' => '成功分配代理商'.$res['num'].'个激活码'
                ]);
            }else {
                throw new LoginException([
                    'msg' => '下发激活码失败'
                ]);
            }
        }

        $list = OrderModel::name('agent_order')->alias('a')
            ->join('cd_agent b', 'a.aid = b.id')
            ->join('cd_wxuser c', 'a.aid = c.uid')
            ->field('a.create_time,a.update_time,c.headimgurl,b.name,b.id,a.order_num,a.num,a.is_delivery')
            ->where(array('order_num' => $order_num))
            ->find();

        $num = Key::where('is_active', 'eq', '1')->where(array('agent_id' => session('agentId')))->count();
        $this->assign('total_key', Key::where('is_active', 'neq', '-1')->where(array('agent_id' => session('agentId')))->count());
        $this->assign('is_active', $num);
        $this->assign('list', $list);

        return view();
    }

    //代理商已发货页面
    public function hadDelivery() {
        $condition = array(
            'a.uper_id' => session('agentId'),
            'a.is_delivery' => 1,
            'a.status' => 1,
        );
        $list = $this->getAgentInfo($condition);

        $this->assign('list', $list);

        return view();
    }

    //代理商激活码仓库
    public function keyInventory() {
        $list = Key::name('key')->alias('a')
            ->join('cd_user b', 'a.uid = b.id')
            ->join('cd_wxuser c', 'a.uid = c.uid')
            ->where(array('a.agent_id' => session('agentId'), 'a.is_active' => 1, 'c.type' => 0))
            ->select();

        $key = Key::where('is_active', 'eq', '1')->where(array('agent_id' => session('agentId')))->select();

        $this->assign('list', $list);
        $this->assign('key', $key);
        return view();
    }

    public function keyInventory1() {
        $key = Key::where('is_active', 'eq', '0')->where(array('agent_id' => session('agentId')))->select();
        $this->assign('keys', $key);
        return view();
    }

    //确认发货
    public function deliveryCode() {
        if(Request::instance()->post()){
            $data = Request::instance()->post();
            //先把激活码更新到用户名下
            $res = (new Key())->distributeCode($data['id'], $data['name'], $data['num']);

            if(intval($res['num']) > 0) {
                OrderModel::where('order_num', $data['order_num'])->update(array('is_delivery' => 1));
                throw new SuccessMessage([
                    'msg' => '成功分配代理商'.$res.'个激活码'
                ]);
            }else {
                throw new LoginException([
                    'msg' => '下发激活码失败'
                ]);
            }
        }
    }

    //获取代理商的订单，微信等信息
    public function getAgentInfo($condition = array()) {
        $list = OrderModel::name('agent_order')->alias('a')
            ->join('cd_agent b', 'a.aid = b.id')
            ->join('cd_wxuser c', 'a.aid = c.uid')
            ->field('a.create_time,c.headimgurl,b.name,b.id,a.order_num,a.num')
            ->order('a.create_time','DESC')
            ->where($condition)
            ->select();

        return $list;
    }
}